package cn.iocoder.yudao.module.promotion.coupon.convert;

import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.promotion.activity.enums.PromotionProductScopeEnum;
import cn.iocoder.yudao.module.api.promotion.coupon.dto.CouponRespDTO;
import cn.iocoder.yudao.module.api.promotion.coupon.enums.CouponStatusEnum;
import cn.iocoder.yudao.module.api.promotion.coupon.enums.CouponTemplateValidityTypeEnum;
import cn.iocoder.yudao.module.promotion.coupon.dal.dataobject.CouponRecordDO;
import cn.iocoder.yudao.module.promotion.coupon.dal.dataobject.CouponTemplateDO;
import cn.iocoder.yudao.module.promotion.coupon.vo.AppCouponPageReqVO;
import cn.iocoder.yudao.module.promotion.coupon.vo.CouponRecordPageItemRespVO;
import cn.iocoder.yudao.module.promotion.coupon.vo.CouponRecordPageReqVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 优惠劵领取记录 Convert
 *
 * @author 芋道源码
 */
@Mapper
public interface CouponRecordConvert {

    CouponRecordConvert INSTANCE = Mappers.getMapper(CouponRecordConvert.class);

    PageResult<CouponRecordPageItemRespVO> convertPage(PageResult<CouponRecordDO> page);

    CouponRespDTO convert(CouponRecordDO bean);

    default CouponRecordDO convert(CouponTemplateDO template, Long userId) {
        CouponRecordDO couponRecordDO = new CouponRecordDO();
        couponRecordDO.setTemplateId(template.getId());
        couponRecordDO.setName(template.getName());
        couponRecordDO.setTakeType(template.getTakeType());
        couponRecordDO.setUsePrice(template.getUsePrice());
        couponRecordDO.setProductScope(template.getProductScope());
        if (Objects.equals(template.getProductScope(), PromotionProductScopeEnum.VERIFY.getScope())) {
            // 核销券，随机一个核销码，长度为 8 位数
            Long verifyCode = RandomUtil.randomLong(10000000L, 99999999L);
            couponRecordDO.setProductScopeValues(List.of(verifyCode));
        } else {
            couponRecordDO.setProductScopeValues(template.getProductScopeValues());
        }
        couponRecordDO.setDiscountType(template.getDiscountType());
        couponRecordDO.setDiscountPercent(template.getDiscountPercent());
        couponRecordDO.setDiscountPrice(template.getDiscountPrice());
        couponRecordDO.setDiscountLimitPrice(template.getDiscountLimitPrice());
        couponRecordDO.setStatus(CouponStatusEnum.UNUSED.getStatus());
        couponRecordDO.setUserId(userId);
        if (CouponTemplateValidityTypeEnum.DATE.getType().equals(template.getValidityType())) {
            couponRecordDO.setValidStartTime(template.getValidStartTime());
            couponRecordDO.setValidEndTime(template.getValidEndTime());
        } else if (CouponTemplateValidityTypeEnum.TERM.getType().equals(template.getValidityType())) {
            couponRecordDO.setValidStartTime(LocalDateTime.now().plusDays(template.getFixedStartTerm()));
            couponRecordDO.setValidEndTime(LocalDateTime.now().plusDays(template.getFixedEndTerm()));
        }
        return couponRecordDO;
    }

    CouponRecordPageReqVO convert(AppCouponPageReqVO pageReqVO, Collection<Long> userIds);

}
